const BaseRest = require('./base.js');

module.exports = class extends BaseRest {
  async getAction() {
    let data = {};
    const map = {};
    // 获取详情
    if (this.id) {
      map.school_code = this.id;
      data = await this.modelInstance.where(map).find();
      return this.success(data);
    }

    // 关键词，支持学校代码，学校名称，城市代码，城市名称的模糊匹配，要过滤不安全的字符
    const key = this.get('key');
    if (key) {
      map['school_code|school_name|city_code|city_name'] = ['like', '%' + key + '%'];
    }

    // 按照城市代码搜索
    const city_code = this.get('city_code');
    if (city_code) {
      map['city_code'] = city_code;
    }

    const page = this.get('page') || 1; // 页码
    const pageSize = this.get('pageSize') || 10; // 每页显示数量
    data = await this.modelInstance.where(map).page(page, pageSize).order('school_code asc').countSelect();
    return this.success(data);
  }

  /**
     * 删除内容
     * @return {[type]} [description]
     */
  async deleteAction() {
    if (!this.id) {
      return this.fail(1001, '记录不存在');
    }
    const rows = await this.modelInstance.where({
      school_code: this.id
    }).delete();
    if (rows) {
      // await this.hook('contentDelete', {id: this.id});
      return this.success({
        affectedRows: rows
      }, '删除成功');
    } else {
      return this.fail(1000, '删除失败');
    }
  }
};
